package com.kgc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kgc.dao.AddressMapper;
import com.kgc.entity.Address;
import com.kgc.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author :qinya
 * @PackageName:com.kgc.service.impl
 * @ClassName: AddressServiceImpl
 * @Description: TODO
 * @date : 2025/5/20,21:02
 */
@Service
@Transactional
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<Address> selectByPage(Map<String, Object> params) {
        PageHelper.startPage((int) (params.get("pageNum")), (int) (params.get("pageSize")), "isDefault desc,createTime desc");
        List<Address> list = addressMapper.selectAllAddress((int) params.get("userId"));
        return new PageInfo<>(list);
    }

    @Override
    public boolean editAddress(Address address) {
        return addressMapper.updateAddress(address) == 1;
    }

    @Override
    public boolean addAddress(Address address) {
        return addressMapper.insertAddress(address) == 1;
    }

    @Override
    public boolean delAddress(int id) {
        return addressMapper.deleteAddress(id) == 1;
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public Address selectById(int id) {
        return addressMapper.selectAddressById(id);
    }

    @Override
    public boolean cancelDefaultAddress(int id) {
        return addressMapper.cancelDefaultAddress(id) == 1;
    }

    @Override
    public boolean setDefaultAddress(int id) {
        return addressMapper.setDefaultAddress(id) == 1;
    }
}
